const service = require('./fansDetailService.js')
var page = 0
var opt
var tit
var id
var Types
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fansListInfo: null,
    items: [],
    hasNextPage: false,
    startPage: 0,  //开始页
    nextPage: 1,
    loadMoreData: '加载更多……',
    isLoading: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    opt = options
    console.log("^^^^^^^^^^" + options.title, options.user_id, options.type)  
    tit = options.title
    Types = options.type
    if (Types == 2){
      this.otherFansService(opt)
    }else{
      this.fansService()
    }
  },
  /**
 * view触底监听
 */
  onReachBottom: function () {
    var self = this;
    // 当前页是最后一页
    if (!self.data.hasNextPage) {
      self.setData({
        loadMoreData: '暂无更多数据'
      })
      return;
    }
    setTimeout(function () {
      console.log('上拉加载更多');
      self.setData({
        hideBottom: false
      })
      if (Types == 2) {
        self.otherFansService(opt)
      } else {
        self.fansService()
      }
    }, 300);
  },
  //粉丝
  fansService: function () {
    var that = this;
    var parma = {
      userId: getApp().globalData.userId,
      startPage: that.data.startPage
    };
    service.requestFans(parma, function (data) {
      var array = data.items;
      that.setData({
        startPage: data.nextPage,
      })
      for (var i = 0; i < data.items.length; i++) {
        if (data.items[i].isFouces == 0){
          that.setData({
            focus: "关注",
          })
        }else{
          that.setData({
            focus: "已关注",
          })
        }
      }
      // if (that.data.startPage == 0) {
      //   that.setData({
      //      dataSource1: data, items: array, hasNextPage: data.hasNextPage 
      //   })
      // } else {
      that.setData({
        dataSource1: data, items: that.data.items.concat(array), hasNextPage: data.hasNextPage, isLoading:false});
      // }
      wx.setNavigationBarTitle({
        title: tit + "的粉丝"
      })
    });
  },
  otherFansService: function (options) {
    id = options.user_id
    var that = this;
    var parma = {
      userId: getApp().globalData.userId,
      thirdUserId: id,
      startPage: that.data.startPage
    };
    service.requestOtherFans(parma, function (data) {
      var array = data.items;
      that.setData({
        startPage: data.nextPage
      })
      for (var i = 0; i < data.items.length; i++) {
        if (data.items[i].isFouces == 0) {
          that.setData({
            focus: "关注",
          })
        } else {
          that.setData({
            focus: "已关注",
          })
        }
      }
      if (that.data.startPage == 0) {
        that.setData({ dataSource1: data, items: array, hasNextPage: data.hasNextPage, isLoading: false  });    
      } else {
        that.setData({
          dataSource1: data, items: that.data.items.concat(array), hasNextPage: data.hasNextPage, isLoading: false,
 });
      }
      wx.setNavigationBarTitle({
        title: tit + "的粉丝"
      })
    });
  },
  submitForm: function(e){
    // 获取表单id
    let formId = e.detail.formId;
    getApp().saveUserFormId(formId, 12);
  }, 

  fansBtn: function (e) {
    var that = this;
    console.log(e.currentTarget.dataset.userid)
    let idx = e.currentTarget.dataset.idx;
    if (that.data.items[idx].isFouces == 0){
      this.addFansService(e.currentTarget.dataset.userid.userId,0, idx)
      that.setData({
        
      })
    }else{
      this.addFansService(e.currentTarget.dataset.userid.userId, 1, idx)
      that.setData({
        
      })
    }
  },
  //添加取消粉丝
  addFansService: function (focusid,state, idx) {
    var that = this;
    var parma = {
      userId: getApp().globalData.userId,
      foucesUserId: focusid,
      flag: state  //关注状态，0关注目标用户，1取消对目标用户的关注
    };
    service.requestAddFans(parma, function (data) {
      // if (Types == 2) {
      //   that.fansService()
      // } else {
      //   that.otherFansService(opt)
      // }
      if (state == 0) {
        that.data.items[idx].fansCount ++;
      that.data.items[idx].isFouces = 1;
        that.setData({
          items : that.data.items,
        })
      } else {
        that.data.items[idx].fansCount --;
        that.data.items[idx].isFouces = 0;
        that.setData({
          items: that.data.items
        })
      }
    },function(err){
      wx.showToast({
        title: err.data.message,
        icon:"none"
      })
    });
  },
  //item点击事件
  fansClick: function (e) {
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '/mineSubs/pages/homepage/personalhomepage?userId=' + e.currentTarget.dataset.id.userId,
    })
  }

  
})
